//
//  YDMineViewController.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2017/11/3.
//  Copyright © 2017年 LK-IOS. All rights reserved.
//

#import "YDMineViewController.h"
#import "YDMineControllerCell.h"
#import "YDMineControllerTopView.h"
#import "YDMineControllerUnLoginTopView.h"
#import "YDRegisterController.h"
#import "YDAccountSettingController.h"
#import "YDMinePositionController.h"
#import "YDChoosePayViewController.h"
#import "YDWithdrawalController.h"
#import "YDFundDetailsController.h"//资金明细

#import "YDTuiGuangViewController.h"
#import "YDSettingViewController.h"
#import "YDBaseMessageViewController.h"
#import "YDBecomeBrokerController.h"//经纪人
#import "YDRealNameAuthController.h"//实名认证


@interface YDMineViewController ()<UITableViewDelegate , UITableViewDataSource , SYMineControllerTopViewDelegate , SYMineControllerUnLoginTopViewDelegate>

//tableView
@property (nonatomic , weak) UITableView *tableView;
//YDMineControllerUnLoginTopView
@property (nonatomic , weak) YDMineControllerUnLoginTopView *unLoginView;
//YDMineControllerTopView
@property (nonatomic , weak) YDMineControllerTopView *topView;

@property (nonatomic, strong)NSNumber *isHaveOutMessage; //是否有站外未读消息
@property (nonatomic, strong)NSNumber *isHaveInMessage; //站内消息



@end

@implementation YDMineViewController


-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)init
{
    if (self = [super init]) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showRedPoint:) name:kShowRedPointNoti object:nil];
        [self requestUnreadMessage];
    }
    return self;
    
}


/**
收到通知是否显示小红点
 */
- (void)showRedPoint:(NSNotification *)noti

{
  
    self.topView.notiView.isHide = ![noti.object boolValue];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;

    self.view.backgroundColor = NumberColor(247, 247, 247);
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self tableView];

}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
    if ([SYUserInformation sharedInstance].isLogin) {
        
        [self requestUnreadMessage];
        [self topView];

        [self refreshUserInfo];
        
    }else{
        [self unLoginView];
    }
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (![SYUserInformation sharedInstance].isLogin) {
         [self unLoginView];
    }
}

/**
 请求未读消息数
 */
-(void)requestUnreadMessage
{
    if ([[SYUserDataManger sharedInstance] userTokenIsOver]) {return;};

    [BaseNetRequest sessionRequestWithparameters:nil method:GETMethod requstType:GetUnreadMessage WithSuccessBlock:^(NSDictionary *resultDict) {
        if ([resultDict[@"code"] integerValue] == 200) {
            
            self.isHaveOutMessage  = resultDict[@"result"][@"hasOutsideMessage"];
            self.isHaveInMessage =  resultDict[@"result"][@"hasInsideMessage"];
    
            if (![self.isHaveOutMessage boolValue] && ![self.isHaveInMessage boolValue]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:kShowRedPointNoti object:@(NO)];
            }else
            {
                 [[NSNotificationCenter defaultCenter] postNotificationName:kShowRedPointNoti object:@(YES)];
            }
        }
    } failBlock:^(NSError *error) {
        //失败不做处理
        
    }];
}

-(void)refreshUserInfo
{
    [BaseNetRequest sessionRequestWithparameters:nil method:GETMethod requstType:GetUerInfo WithSuccessBlock:^(NSDictionary *resultDict) {
        if ([resultDict[@"code"] integerValue] == 200) {
            //成功刷新用户信息
            if (((NSDictionary *)resultDict[@"result"]).count) {
                
               [[SYUserDataManger sharedInstance] storeUserDataWithDict:resultDict[@"result"]];
                
               self.topView.dict = resultDict[@"result"];
                
            }
           
        }
    } failBlock:^(NSError *error) {
        //失败不做处理
        
    }];
}

-(YDMineControllerUnLoginTopView *)unLoginView
{
    if (_unLoginView == nil) {
        CGRect topViewFrame;
        if (isPhoneX) {
            topViewFrame = CGRectMake(0, 0, LKScreenW, rateX812(267));
        }else{
            topViewFrame = CGRectMake(0, 0, LKScreenW, rate750(267));
        }
        YDMineControllerUnLoginTopView *unLoginView = [[YDMineControllerUnLoginTopView alloc] init];
        unLoginView.frame = topViewFrame;
        unLoginView.delegate = self;
        self.tableView.tableHeaderView = unLoginView;
    }
    return _unLoginView;
}
/*******SYMineControllerUnLoginTopViewDelegate***********/

-(void)mineControllerUnLoginTopView:(YDMineControllerUnLoginTopView *)unTopView clickStyle:(SYMineUnLoginTopViewClickStyle)style
{
    if (style == SYMineUnLoginTopViewIcon) {

    }else if (style == SYMineUnLoginTopViewClickLogin){//去登陆
        
        YDLoginController *vc = [[YDLoginController alloc] init];
        vc.hidesBottomBarWhenPushed  = YES;
        [self.navigationController pushViewController:vc animated:YES];

    }else if (style == SYMineUnLoginTopViewClickRegister){//去注册
        YDRegisterController *vc = [[YDRegisterController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
/******YDMineControllerTopView*******/
-(YDMineControllerTopView *)topView
{
    if (_topView == nil) {
        CGRect topViewFrame;
        if (isPhoneX) {
            topViewFrame = CGRectMake(0, 0, LKScreenW, rateX812(267));
        }else{
            topViewFrame = CGRectMake(0, 0, LKScreenW, rate750(267));
        }
        YDMineControllerTopView *topView = [YDMineControllerTopView mineControllerTopViewInitWithFrame:topViewFrame andDict:[[SYUserDataManger sharedInstance] getUserDict]];
       
        topView.frame = topViewFrame;
        topView.delegate = self;
        self.topView = topView;
        self.tableView.tableHeaderView = topView;

    }
    return _topView;
}

//wd-chengweijingjiren
/**********SYMineControllerTopViewDelegate**************/
-(void)mineControllerTopView:(YDMineControllerTopView *)topView clickStyle:(SYMineTopViewClickStyle)style
{
    if (style == SYMineTopViewClickAcc) {//账号设置
        
        YDAccountSettingController *vc = [[YDAccountSettingController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (style == SYMineTopViewClickCharge){//充值
        
        [self loadRealNameInformation:YES];

        
//        YDChoosePayViewController *vc = [[YDChoosePayViewController alloc] init];
//        vc.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (style == SYMineTopViewClickWithButton){//提现
//        [self loadRealNameInformation:NO];
        YDWithdrawalController *vc = [[YDWithdrawalController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (style == SYMineTopViewClickWithNoti)//消息
    {
        YDBaseMessageViewController *vc = [[YDBaseMessageViewController alloc] init];
        if (!self.isHaveOutMessage && self.isHaveInMessage) {
            vc.type = PZMessage;
        }
        [self.navigationController pushViewController:vc animated:YES];
    }
}
/*******tableView********/
-(UITableView *)tableView
{
    if (_tableView == nil) {
        
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, LKScreenW, self.view.height - self.tabBarController.tabBar.height) style:UITableViewStylePlain];
        [self.view addSubview:tableView];
        tableView.backgroundColor = NumberColor(247, 247, 247);
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.estimatedRowHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        tableView.estimatedSectionHeaderHeight = 0;
        tableView.scrollEnabled = NO;
        if (isIos11) {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView = tableView;
    }
    return _tableView;
}

#pragma UITableViewDelegate , UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 1;
    }else{
        return 1;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return isPhoneX ? rateX812(10) : rate750(10);
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return isPhoneX ? rateX812(50) : rate750(50);;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0f;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YDMineControllerCell *cell = [YDMineControllerCell cellWithTableView:tableView];
    if (indexPath.section == 0) {
        cell.imageName = @"wd-zijinmingxi";
        cell.cellName = @"资金明细";
    }
    
    else if (indexPath.section == 1)
    {
        cell.imageName = @"wd-shezhi";
        cell.cellName = @"设置";
    }else{
        cell.imageName = @"user_protocol_risk";
        cell.cellName = @"用户须知及风险提示";
    }

    return cell;
}
//
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        if (![SYUserInformation sharedInstance].isLogin){
            [self unLoginPost];
            return;
        }
        //资金明细
        YDFundDetailsController *vc = [[YDFundDetailsController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
        
    }else if (indexPath.section == 1){//推广赚钱  ，成为经纪人，后台给接口，是不是经纪人及经纪人状态进入不同界面
        
        YDSettingViewController *vc = [[YDSettingViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];

    }else if (indexPath.section == 2)//用户风险须知
    {
        
        YDAgreementController *vc = [[YDAgreementController alloc] init];
        vc.agreementCategory = UserNoticeRisk;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
    }
}


//未登录提示
-(void)unLoginPost
{
    LK(weakSelf)
    PZTipsAlertView *alert = [PZTipsViewManger shareSingleTipsAlertViewTitle:@"提示" Mesage:@"请先登录操作" ButtonTitle:@"我知道了" ButtonClickBlock:^{
        
        [weakSelf.pz_popupController dismiss];
    }];
    self.pz_popupController = [[PZPopUpController alloc] init];
    [self.pz_popupController dropAnimatedWithRotateAngle:30];
    [self.pz_popupController presentContentView:alert duration:0.75 springAnimated:YES];
}

//未认证提示
-(void)unRealName
{
    LK(weakSelf)
    PZTipsAlertView *alert = [PZTipsViewManger shareDoubleTipsAlertViewTitle:@"提示" Mesage:@"您尚未进行实名认证，请先进行认证" CancleTitle:@"取消" SureTitle:@"确定" CancleBlock:^{
        [weakSelf.pz_popupController dismiss];
        
    } SureBlock:^{
        [weakSelf.pz_popupController dismiss];
        
        YDRealNameAuthController *vc = [[YDRealNameAuthController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    weakSelf.pz_popupController = [[PZPopUpController alloc] init];
    [weakSelf.pz_popupController dropAnimatedWithRotateAngle:30];
    [weakSelf.pz_popupController presentContentView:alert duration:0.75 springAnimated:YES];
}



/**
 获取实名认证信息
 @param chargeOrDraw YES充值  NO提现
 */
-(void)loadRealNameInformation:(BOOL)chargeOrDraw
{
    [[SYProgressHUD sharedInstance] showMBHudOnlyLoading];
    [BaseNetRequest sessionRequestWithparameters:nil method:GETMethod requstType:GetSettingInformationList WithSuccessBlock:^(NSDictionary *resultDict) {
        [[SYProgressHUD sharedInstance] dismissMBWithTime:0];
        
        DDLog(@"resultDict = %@" , resultDict)
        if ([resultDict[@"code"] integerValue] == 200) {
            
            if (![resultDict[@"result"][@"settingRealName"] boolValue]) {
                [self unRealName];
            }else{
                if (chargeOrDraw) {
                    DDLog(@"充值")
                    YDChoosePayViewController *vc = [[YDChoosePayViewController alloc] init];
                    vc.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:vc animated:YES];
                }else{
                    DDLog(@"提现")
                    YDWithdrawalController *vc = [[YDWithdrawalController alloc] init];
                    vc.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:vc animated:YES];
                }
            }
        }else{
            [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"获取实名信息出错" isExistNav:NO];
        }
    } failBlock:^(NSError *error) {
        [[SYProgressHUD sharedInstance] dismissMBWithTime:0];
        
        [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:@"获取实名信息出错" isExistNav:NO];
    }];
}





@end
